from rest_framework.pagination import PageNumberPagination
from django.db import models


class Pagination(PageNumberPagination):
    """ 分页设置 """
    page_size_query_param = 'limit'
    page_query_param = 'page'


class BaseModel(models.Model):
    """
    用于model类，为每个model新增创建时间和更新时间字段
    """
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True)
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间', null=True)

    class Meta:
        abstract = True


class CustomError(Exception):
    """
    自定义异常，用于excel表，reason表示异常信息
    """

    def __init__(self, reason):
        self.reason = reason


def Q2B(uchar):
    """单个字符 全角转半角"""
    inside_code = ord(uchar)
    if inside_code == 0x3000:
        inside_code = 0x0020
    else:
        inside_code -= 0xfee0
    if inside_code < 0x0020 or inside_code > 0x7e:  # 转完之后不是半角字符返回原来的字符
        return uchar
    return chr(inside_code)
